package com.wzt.lianfirecontrol.activity;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.service.PlayMp3Service;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.PreferencesUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;

import org.json.JSONObject;

import java.io.IOException;

/**
 * 推送报警页面
 */
public class AlarmTranActivity extends BaseActivity {

    private RelativeLayout rl_content;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_alarm_tran);
        rl_content = findViewById(R.id.rl_content);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle && UserInfoModel.isLogin(this)){
            String bannerStr = bundle.getString("pushBannerModle");
            if (!StringUtils.isEmpty(bannerStr)){
                try {
                    BannerModel bannerModel = JSONUtil.jsonObjectToBean(new JSONObject(bannerStr), BannerModel.class);


                    if (ConstData.ALARM.equals(bannerModel.getActionType())) {
                        // actionValue 0火警 1故障 2监管 3预警 （只有火警有警报画面）
                        if ("0".equals(bannerModel.getActionValue())) {
                            // 火警
                            ImageView iv_img = findViewById(R.id.iv_img);
                            iv_img.setVisibility(View.VISIBLE);
                            GildeUtils.loadImageAsGif(this, iv_img, R.drawable.icon_baojing);
                            rl_content.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (!StringUtils.isEmpty(bannerModel.getBuildingId())){
                                        Bundle skipToBundle = new Bundle();
                                        skipToBundle.putString("equId", bannerModel.getId());
                                        BannerModel bannerModel = new BannerModel();
                                        bannerModel.setActionType(ConstData.SHOWFRAG);
                                        bannerModel.setActionValue(ConstData.BAOJINGDETAIL);
                                        bannerModel.setActionTitle("详情");
                                        setClickAction(bannerModel, skipToBundle);
                                    }
                                    finish();
                                }
                            });
                            openAssetMusics(bannerModel.getActionValue());
                        }else {
                            // 跳转到首页
                            Intent intent = new Intent(this, PlayMp3Service.class);
                            intent.putExtra("actionValue", bannerModel.getActionValue());
                            startService(intent);
                            finish();
                        }
                        return;
                    }else {
                        setClickAction(bannerModel);
                    }
                }catch (Exception e){}
            }
        }{

        }
        finish();

    }

    /**
     * 打开assets下的音乐mp3文件
     */
    private void openAssetMusics(String actionValue) {
        switch (actionValue){
            case "0":// 报警
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_BAOJING_STATUS, "1"))){
                    return;
                }
                break;
            case "1":// 故障
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_GUZHANG_STATUS, "1"))){
                    return;
                }
                break;
            case "2":// 监管
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_JIANGUAN_STATUS, "1"))){
                    return;
                }
                break;
            case "3":// 预警
                if ("0".equals(PreferencesUtils.getString(this, ConstData.SWITCH_YUJING_STATUS, "1"))){
                    return;
                }
                break;
        }
        try {
            String fileName;
            switch (actionValue){
                case "0":
                    fileName = "huojing.mp3";
                    break;
                case "1":
                    fileName = "guzhang.mp3";
                    break;
                case "2":
                    fileName = "jianguan.mp3";
                    break;
                default:
                    fileName = "yujing.mp3";
                    break;
            }
            //播放 assets/a2.mp3 音乐文件
            AssetFileDescriptor fd = getAssets().openFd(fileName);
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    // 火警语音重复播放
                    if (null != mediaPlayer) {
                        mediaPlayer.start();
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mediaPlayer) {
            mediaPlayer.stop();
            mediaPlayer = null;
        }
    }
}
